#!/usr/bin/perl -w
use strict;
use autodie;
use Math::Trig;

sub p2c {
  die ("Numero erroneo de argumentos\n") if (@_ != 4);
  my %arg = @_;
  $arg{x}     = $arg{r}*cos($arg{angle}) unless (exists $arg{x} or !(defined $arg{r} and defined $arg{angle}));
  $arg{y}     = $arg{r}*sin($arg{angle}) unless (exists $arg{y} or !(defined $arg{r} and defined $arg{angle}));
  $arg{angle} = atan2($arg{y}, $arg{x}) unless (exists $arg{angle} or !(defined $arg{x} and defined $arg{y}));
  $arg{r}     = sqrt($arg{x} * $arg{x} + $arg{y} * $arg{y}) unless (exists $arg{r} or !(defined $arg{x} and defined $arg{y}));
  return %arg;
}

my %a = p2c(r => 2, angle => 0.4);
my ($k, $v);
while (($k, $v) = each %a) {print "$k => $v\n"};
